App({
  globalData: {
    portrait_temp: '',
    drawWidth: 0,
    drawHeight: 0,
    scale: 1,
    windowWidth: 0,
  },
  /**
   * 当小程序初始化完成时，会触发 onLaunch（全局只触发一次）
   */
  onLaunch: function() {
    var res = wx.getSystemInfoSync()
    this.globalData.windowWidth = res.windowWidth;
    this.globalData.drawWidth = res.windowWidth * 0.7;
    this.globalData.drawHeight = res.windowHeight * 0.61;
  },

  //分享到微信朋友圈
  shareToMoments(title, image) {
    wx.showLoading();
    var that = this;
    //缓存canvas绘制小程序二维码
    wx.downloadFile({
      url: "https://api.xinwangd.com/static/code.png",
      success: function(res) {
        //缓存二维码
        that.globalData.qrcode_temp = res.tempFilePath;
        wx.downloadFile({
          url: image + "",
          success: function(res) {
            //缓存图片
            console.log(res.tempFilePath);
            that.globalData.image_temp = res.tempFilePath;
            //开始绘制图片
            that.drawImage(title, image);
          }
        })

      }
    })

  },
  drawImage(title, image) {

    //绘制canvas图片
    var that = this
    const ctx = wx.createCanvasContext('myCanvas')
    var bgPath = "../../images/bg.jpg";
    var portraitPath = that.globalData.portrait_temp
    var qrPath = that.globalData.qrcode_temp
    var imagePath = that.globalData.image_temp
    var drawWidth = that.globalData.drawWidth
    var drawHeight = that.globalData.drawHeight
    that.globalData.scale = 1.6;
    //绘制背景
    ctx.setFillStyle('#fff')
    ctx.fillRect(0, 0, drawWidth, drawHeight)
    //绘制二维码
    ctx.drawImage(qrPath, 0, 0.73 * drawHeight, 0.3 * drawWidth, 0.3 * drawWidth)

    //绘制文字
    var chr = title.split(""); //这个方法是将一个字符串分割成字符串数组
    var temp = "";
    var row = [];
    ctx.setFontSize(16)
    ctx.setFillStyle("#000")
    for (var a = 0; a < chr.length; a++) {
      if (ctx.measureText(temp).width < 0.6 * drawWidth) {
        temp += chr[a];
      } else {
        a--; //这里添加了a-- 是为了防止字符丢失，效果图中有对比
        row.push(temp);
        temp = "";
      }
    }
    row.push(temp);
    //如果数组长度大于2 则截取前两个
    if (row.length > 2) {
      var rowCut = row.slice(0, 2);
      var rowPart = rowCut[1];
      var test = "";
      var empty = [];
      for (var a = 0; a < rowPart.length; a++) {
        if (ctx.measureText(test).width < 0.6 * drawWidth) {
          test += rowPart[a];
        } else {
          break;
        }
      }
      empty.push(test);
      var group = empty[0] + "..." //这里只显示两行，超出的用...表示
      rowCut.splice(1, 1, group);
      row = rowCut;
    }
    for (var b = 0; b < row.length; b++) {
      ctx.fillText(row[b], 0.3 * drawWidth, 0.8 * drawHeight + b * 20);
    }
    ctx.setFontSize(14)
    ctx.setFillStyle("#d43d3d")
    ctx.fillText('长按识别二维码查看全文', 0.3 * drawWidth, 0.92 * drawHeight);
    //绘制图片
    ctx.drawImage(imagePath, 0, 0, drawWidth, 0.7 * drawHeight)
    ctx.draw();
    wx.hideLoading();
  },

  //将绘制的图片设置大小后保存图片到本地
  canvasToImage() {
    var that = this
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: that.globalData.windowWidth,
      height: that.globalData.windowWidth * that.globalData.scale,
      canvasId: 'myCanvas',
      success: function(res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(result) {
            wx.showModal({
              title: '成功保存图片',
              content: '已成功为您保存图片到手机相册，请自行前往朋友圈分享',
              showCancel: false,
              confirmText: '知道了'
            })
          }
        })
      }
    })
  }

})